import request from "@/api/index";
interface dataSourceType {
  [propName: string]: any;
}
export const checkNameForm = async (_rule: dataSourceType = {}, value: any) => {
  if (!value && value != '0' && _rule.required && !_rule.fullField) {// 必填项验证非空并排除非必须项
    return Promise.reject(`${_rule.msg1}`);
  }
  if (_rule.value && _rule.value == _rule.fullField) {// 没变化没校验的必要
    return Promise.resolve()
  }
  if (_rule.api && _rule.fullField) { // 没填值没有校验的必要
    var params = deleteKey(_rule.key, _rule)
    let res = await request[`${_rule.api}`](params);
    if (!res) {
      return Promise.reject(`${_rule.msg2}`);
    } else {
      return Promise.resolve();
    }
  }

};
const deleteKey = (key: string, obj: dataSourceType = {}) => {
  let params: dataSourceType = {};
  params[key] = obj.fullField;
  if (key == 'siteDomain' || key == 'siteDomainAlias') {
    params['siteProtocol'] = obj.params['siteProtocol']
  }
  return params
}